package mudfox.support;

import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;

import com.mongodb.client.result.DeleteResult;
import org.bson.Document;

import java.util.List;
import java.util.HashMap;
import java.util.Map;

public class MongoHelper {
    private MongoDatabase db;
    private MongoCollection<Document> collection;

    /*
     * // or MongoClient client = new MongoClient( "localhost" );
     * // or MongoClient client = new MongoClient( "localhost" , 27017);
     * // or use a connection string MongoClientURI connectionString = new
     * MongoClientURI(
     * "mongodb://localhost:27017,localhost:27018,localhost:27019"); MongoClient
     * client = new MongoClient(connectionString);
     */

    public MongoHelper(String dbName) {
        db = new MongoClient().getDatabase(dbName);
    }

    public MongoHelper(String dbName, String collName) {
        db = new MongoClient().getDatabase(dbName);
        collection = db.getCollection(collName);
    }

    public void setCollection(String collName) {
        collection = db.getCollection(collName);
    }

    public void insertOne(Document doc) {
        collection.insertOne(doc);
    }

    public void insertMany(List<Document> documents) {
        collection.insertMany(documents);
    }

    public long countDocuments() {
        return collection.countDocuments();
    }

    public void dropCollection() {
        collection.drop();
    }

    public Map<String, String> deleteOne(String key, Object value) {
        DeleteResult result = collection.deleteOne(new Document(key, value));
        Map<String, String> map = new HashMap<>();
        map.put("reply", String.valueOf(result.getDeletedCount()));
        return map;
    }

    public Map<String, String> deleteOne(Document filter) {
        DeleteResult result = collection.deleteOne(filter);
        Map<String, String> map = new HashMap<>();
        map.put("reply", String.valueOf(result.getDeletedCount()));
        return map;
    }

    public Map<String, String> deleteMany(String key, Object value) {
        DeleteResult result = collection.deleteMany(new Document(key, value));
        Map<String, String> map = new HashMap<>();
        map.put("reply", String.valueOf(result.getDeletedCount()));
        return map;
    }

    public Map<String, String> deleteMany(Document filter) {
        DeleteResult result = collection.deleteMany(filter);
        Map<String, String> map = new HashMap<>();
        map.put("reply", String.valueOf(result.getDeletedCount()));
        return map;
    }

    public Document findOne() {
        return collection.find().first();
    }

    public Document findOneAndDelete(String key, Object value) {
        return collection.findOneAndDelete(new Document(key, value));
    }

    public FindIterable<Document> find(String key, Object value) {
        return collection.find(new Document(key, value));
    }

    public FindIterable<Document> find(Document filter) {
        return collection.find(filter);
    }

    public FindIterable<Document> findAll() {
        return collection.find();
    }

    public void printAll() {
        try (MongoCursor<Document> cursor = collection.find().iterator()) {
            while (cursor.hasNext()) {
                System.out.println(cursor.next().toJson());
            }
        }
    }
}
